﻿using SRB.Tg.Common;
using SRB.Tg.DrawingStandard;
using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public class GuardbarStructure
    {

        System.Data.DataTable dtProject = new System.Data.DataTable();
        System.Data.DataTable dtBridge = new System.Data.DataTable();
        System.Data.DataTable dtBridgeDrawing = new System.Data.DataTable();
        System.Data.DataTable dtProjectConfig = new System.Data.DataTable();

        string _bridgeMainId = null;
        DrawingConfigModel drawingConfigModel = new DrawingConfigModel();
        string _bridgeId = "";


        System.Data.DataTable dtAviationBridge = new System.Data.DataTable();
        System.Data.DataTable dtAviationBridgeInfo = new System.Data.DataTable();
        System.Data.DataTable dtSection = new System.Data.DataTable();
        System.Data.DataTable dtUpperStructural = new System.Data.DataTable();
        System.Data.DataTable dtDeckStructural = new System.Data.DataTable();
        System.Data.DataTable dtGuardbarStructural = new System.Data.DataTable();
        System.Data.DataTable dtLightingStructural = new System.Data.DataTable();
        System.Data.DataTable dtSteelMaterialCategory = new System.Data.DataTable();
        double bridgeW = 2500;
        double divCap = 2500;
        double divHeight = 2500;
        double bridgeSlope = 0;
        double bridgeLength = 0;
        //double slope = 0;
        public GuardbarStructure(string bridgeId,  out System.Data.DataTable dtProjectTb)
        {
            #region  参数数据初始化
            dtBridgeDrawing = new System.Data.DataTable();//图纸目录信息表
            //传入bridgeId，查询出mainId
            string sqlMainId = "select * from busi_bridge where id='" + bridgeId + "' ";
            _bridgeId = bridgeId;
            string mainId = MySqlHelper.ExecuteDataTable(sqlMainId, CommandType.Text).Rows[0]["main_id"].ToString();
            _bridgeMainId = mainId;
            string sqlBridge = "select * from busi_bridge_main where id='" + mainId + "'";
            dtBridge = MySqlHelper.ExecuteDataTable(sqlBridge, CommandType.Text);

            int bridge_scope = 1;//桥幅（单幅桥和双幅桥）
            if (dtBridge.Rows.Count > 0)
            {
                bridge_scope = int.Parse(dtBridge.Rows[0]["bridge_scope"].ToString());            
            }


            string sqlDrawing = "select * from busi_bridge_catalog where  bridge_main_id='" + mainId + "'";
            dtBridgeDrawing = MySqlHelper.ExecuteDataTable(sqlDrawing, CommandType.Text);

            string sqlBridge1 = "select * from vi_bridge where  id='" + mainId + "'";
            dtAviationBridgeInfo = MySqlHelper.ExecuteDataTable(sqlBridge1, CommandType.Text);
            if (dtAviationBridgeInfo.Rows.Count > 0)
            {
                string sqlSpan = "select * from vi_aviation_bridge where  bridge_id='" + dtAviationBridgeInfo.Rows[0]["bridge_id"].ToString() + "' order by station asc";
                dtAviationBridge = MySqlHelper.ExecuteDataTable(sqlSpan, CommandType.Text);


                //主梁布跨信息
                string sqlSection = "select * from vi_aviation_section where  bridge_id='" + dtAviationBridgeInfo.Rows[0]["bridge_id"].ToString() + "' ";
                dtSection = MySqlHelper.ExecuteDataTable(sqlSection, CommandType.Text);

                //上部结构信息
                string sqlUpper = "select * from vi_bridge_upper_part where  bridge_id='" + dtAviationBridgeInfo.Rows[0]["bridge_id"].ToString() + "' ";
                dtUpperStructural = MySqlHelper.ExecuteDataTable(sqlUpper, CommandType.Text);

                bridgeSlope = double.Parse(dtAviationBridgeInfo.Rows[0]["bridge_slope"].ToString()) * 0.01;
                bridgeLength = double.Parse(dtAviationBridgeInfo.Rows[0]["bridge_length"].ToString()) * 100;
            }
            string sql = "select * from busi_steel_material_category ";
            dtSteelMaterialCategory = MySqlHelper.ExecuteDataTable(sqlBridge1, CommandType.Text);
            
            #endregion

            dtProjectTb = dtBridgeDrawing;
        }

        public void InitGuardbarStructure()
        {
            if(dtUpperStructural.Rows.Count>0)
            {
                //桥面板结构信息
                string sql = "select * from busi_portable_steel_bridge_deck_structural where  id='" + dtUpperStructural.Rows[0]["deck_structural"].ToString() + "' ";
                dtDeckStructural = MySqlHelper.ExecuteDataTable(sql, CommandType.Text);

                //护栏结构信息
                sql = "select * from busi_portable_steel_bridge_guardbar_structural where  id='" + dtUpperStructural.Rows[0]["guardbar_structural"].ToString() + "' ";
                dtGuardbarStructural = MySqlHelper.ExecuteDataTable(sql, CommandType.Text);

                //灯信息
                sql = "select * from busi_portable_steel_bridge_lighting_structural where  id='" + dtUpperStructural.Rows[0]["lighting_structural"].ToString() + "' ";
                dtLightingStructural = MySqlHelper.ExecuteDataTable(sql, CommandType.Text);

            }

        }

        public void DrawingGuardbarStructure(Database db, DrawingConfigModel drawingConfigModel, string bridgeId, out System.Data.DataTable dtProjectConfigTb, string catalogid,double _bridgeW, double _divCap,double _divHeight)
        {
            InitGuardbarStructure();
            bridgeW = _bridgeW;
            divCap = _divCap;
            divHeight = _divHeight;
            string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
            System.Data.DataTable dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
            dtProjectConfigTb = dtProjectConfig;
            PictureFrameTools.GetBridgeConfig(dtProjectConfig);
            int OverAllScale = drawingConfigModel.drawing_scale;
            System.Data.DataTable dt1; System.Data.DataTable dt2;
            Extents2d ExtPlan, ExtE, ExtDetail;
            Point2d RefPtPlan, RefPtE, RefPtDetail;

            RefPtE = Point2d.Origin;
            ExtE = new Extents2d(RefPtE, RefPtE);

            try
            {
                PlotElevation(ref db, out ExtE, RefPtE, OverAllScale);
            }
            catch { }

            RefPtPlan = RefPtE.C2D(0, -ExtE.L() -10*OverAllScale);
            ExtPlan = new Extents2d(RefPtPlan, RefPtPlan);

            try
            {
                PlotPlan(ref db, out ExtPlan, RefPtPlan.C2D(), OverAllScale);
            }
            catch { }

            RefPtDetail = RefPtPlan.C2D(ExtPlan.W() * 1.5);
            ExtDetail = new Extents2d(RefPtDetail, RefPtDetail);
            try
            {

            }
            catch { }


            LayoutTools.CreateLayout2LRDiv(db, "图纸", ExtE, ExtPlan, ExtDetail,
               Extensions.leftWidth, 5, Extensions.bottomHeight, 10, OverAllScale);
        }


        // 绘制立面图
        public void PlotElevation(ref Database db, out Extents2d ext, Point2d insertPoint, int scale)
        {
            ext = new Extents2d(insertPoint, insertPoint);
            TextPloter.AddTitle(db,insertPoint, ref ext, "立面", "",  scale);
            Point2d pt = insertPoint.C2D(0, -5 * scale);
            #region 护栏
            if(dtGuardbarStructural.Rows.Count>0)
            {
                double guardbar_height = double.Parse(dtGuardbarStructural.Rows[0]["guardbar_height"].ToString());
                int level_materiel_number = int.Parse(dtGuardbarStructural.Rows[0]["level_materiel_number"].ToString());

                //水平间距
                double steel_h_space= double.Parse(dtGuardbarStructural.Rows[0]["steel_h_space"].ToString());

                string materiel_specs = dtGuardbarStructural.Rows[0]["materiel_specs"].ToString();
                string level_materiel_specs = dtGuardbarStructural.Rows[0]["level_materiel_specs"].ToString();
                string vertical_materiel_specs = dtGuardbarStructural.Rows[0]["vertical_materiel_specs"].ToString();
                //外框架
                double mainDiameter = materiel_specs.IsNullOrWhiteSpace() ? 63 : Double.Parse(materiel_specs.Split('*')[0]);
                double mainTh = materiel_specs.IsNullOrWhiteSpace() ? 5 : Double.Parse(materiel_specs.Split('*')[1]);
         
                //水平架立
                double levelDiameter = level_materiel_specs.IsNullOrWhiteSpace() ? 40 : Double.Parse(level_materiel_specs.Split('*')[0]);
                double levelTh = level_materiel_specs.IsNullOrWhiteSpace() ? 3 : Double.Parse(level_materiel_specs.Split('*')[1]);

                //竖向
                double verticalDiameter = vertical_materiel_specs.IsNullOrWhiteSpace() ? 63 : Double.Parse(vertical_materiel_specs.Split('*')[0]);
                double verticalTh = vertical_materiel_specs.IsNullOrWhiteSpace() ? 5 : Double.Parse(vertical_materiel_specs.Split('*')[1]);

            }
            #endregion

            #region 主梁示意
            #endregion

            #region 标注
            #endregion


        }



        /// <summary>
        /// 绘制平面图
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="insertPoint"></param>
        /// <param name="scale"></param>
        public void PlotPlan(ref Database db, out Extents2d ext, Point2d insertPoint, int scale)
        {
            Point2d CT = insertPoint;


            ext = new Extents2d(CT, CT);

            TextPloter.AddTitle(db, CT, ref ext, "A-A", "", scale);
            #region 主梁
            #endregion

            #region 桥面板（钢格栅）
            #endregion

            #region 灯具扩展区
            #endregion

        }




    }
}
